import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs/Observable';

import { LayerFormsService } from './layer-forms.service';
import { StepTypes } from './steps.model';

@Injectable()
export class StepGuard implements CanActivate {

  constructor(
    private router: Router,
    private layerFormsService: LayerFormsService
  ) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    const step: StepTypes = this.layerFormsService.step;
    if(step === next.routeConfig.path) {
      return true;
    } else {
      const step: StepTypes = this.layerFormsService.nextStep();
      // this.router.navigate([`../${step}`], { relativeTo: this.layerFormsService.routeState })
      return false;
    }
  }
}
